<?php
include("../lib.php");

// connect to mysql

    $sqlservr_con = mysql_connect('127.0.0.1', 'root', 'gbifims');
    if (!$sqlservr_con) {
        die('db server unavailable.');    // . mysql_error());
    }
    $sql_con = mysql_select_db('gbif_ims', $sqlservr_con);
    if (!$sql_con) {
        die ('db unavailable.');    // . mysql_error());
    }

// insert if request
if( $_GET["user_name"])
{
    $user_name = strip_tags($_GET["user_name"]);
	//$email_address = strip_tags($_GET["email_address"]);

	file_put_contents  ( "param_select.txt", $user_name);
	//add the user

	/// Build the SQL
 	$db_query = "SELECT * FROM users WHERE user_name LIKE '%".$user_name."%' AND deleted=0 ORDER BY user_name;";
    
 	file_put_contents  ( "select_statement.txt", $db_query);
 	
 	$result = mysql_query($db_query);
    
    if (!$result) {
        die('invalid query');    // . mysql_error());
    }
	
	$result = mysql_query( $db_query );
}
else 
{
	$db_query = sprintf("SELECT * FROM users WHERE deleted = 0 ORDER BY user_name");
    $result = mysql_query($db_query);
	
    file_put_contents  ( "select_statement.txt", $db_query);
    
    if (!$result) {
        die('invalid query');    // . mysql_error());
    }
	
	$result = mysql_query( $db_query );
}        

/// Return XML
$str.='<results>';
	while ($row = mysql_fetch_assoc($result))
		{
		$str.='<record>';
		$str.='<user_id>'.$row["user_id"].'</user_id>';
		$str.='<user_name>'.$row["user_name"].'</user_name>';
		$str.='<email_address>'.$row["email_address"].'</email_address>';
		$str.='</record>';
		}
$str.='</results>';
   
header ("content-type: text/xml"); 
echo $str;

file_put_contents  ( "out.txt", $str);

mysql_free_result( $result )

?>